/*
* Valentine Gladstein
* SOLAR ID 108731583
* Homework #2
* CSE214
* Recitation 03-Phil Ammirato
* Grading TA: Yanjie LI
*/

import java.io.*;
import java.util.*;

public class CreditCardReport {

    //The CreditCardReport class provides a console-driven menu system for manipulating a CardList object

    private static CardList list;    //Stores the CardList object associated with this CreditCardReport

/**
 * Displays the main menu in the standard output and gets the user's input selection.
 * @return The user's entry, converted to lowercase.
 */
    public static String displayMenu(){

	System.out.println("\n\n---------------------------");
	System.out.println("Main Menu");
	System.out.println("---------------------------");
	System.out.println("IN: Insert new credit card info to the list");
	System.out.println("DA: Display all credit card info in this list");
	System.out.println("DB: Display all credit cards with the selected balance ");
	System.out.println("DC: Display all credit cards issued by the selected company");
	System.out.println("DP: Display all credit cards with the selected name");
	System.out.println("DS: Display all credit cards with the selected status");
	System.out.println("RM: Remove a card from the list");
	System.out.println("TP: Display the top (first) entry in the list");
	System.out.println("NX: Display the credit card after the current card");
	System.out.println("PR: Display the credit card before the current card");
	System.out.println("Q:  Quit");
	System.out.println("---------------------------\n");
	System.out.println("");
	System.out.print("Please make a selection: ");

	String str = getInput();

	return str.toLowerCase();

    }



/**
 * A helper method used internally to read user input from the standard input
 * @return a string containing the user's input
 */
    public static String getInput() {
	String s = "";
	InputStreamReader isr = new InputStreamReader(System.in);
	BufferedReader br = new BufferedReader(isr);
	try {
	    s = br.readLine();

	} catch (IOException e) {}
	return s;
    }
   


/**
 * Provides a menu-driven system for adding a User object to the list.
 *
 */ 
    public static void addUser() {

	String name;
	String company;
	String dateOpened;
	Double balance;
	int status;

	System.out.println("\n");
	System.out.print("Enter name (last first): ");
	name = getInput();
	if (name.length() > 25) {
	    name = name.substring(0,24);
	    System.out.println("Name exceeds maximum length of 25 characters. Name recorded as: " + name);
	}
	System.out.print("Enter issuing company: ");
	company = getInput();
	if (company.length() > 15) {
	    company = company.substring(0,14);
	    System.out.println("Name exceeds maximum length of 15 characters. Company recorded as: " + company);
	}
	System.out.print("Enter date opened (m/d/y): ");
	dateOpened = getInput();
	System.out.print("Enter balance: ");
	try {
	    balance = Double.parseDouble(getInput());
	}
	catch(Exception E) {
	    System.out.println("Please enter a valid numerical value. None of your entries have been recorded.");
	    return;
	}
	System.out.print("Enter Status: ");
	try {
	    status = Integer.parseInt(getInput());
	}
	catch(Exception E) {
	    System.out.println("Please enter a valid integer value. None of your entries have been recorded.");
	    return;
	}

	CardInfo newInfo = new CardInfo(name, company, dateOpened, balance, status);
	list.insertCardInfo(newInfo);
	System.out.println("\nCredit card information added for: " + name) ;
    }


/**
 * Displays all entries in the list. This method calls the CardList class' displayAll() method.
 */
    public static void displayAll() {
	list.displayAll();
    }

/**
 * Displays all entries in the list with a balance >= user's input. After getting the user' input, this method calls the CardList class' displayByBal() method.
 */
    public static void displayByBal() {
	double bal;
	System.out.print("Enter balance: ");
	try {
	    bal = Double.parseDouble(getInput());
	}
	catch (Exception e) {
	    System.out.println("Please enter a valid numerical value for the balance.");
	    return;
	}
	list.displayByBalance(bal);
    }

/**
 * Displays all entries in the list with a compnay equal to urser's input. After getting the user' input, this method calls the CardList class' displayByCompany() method.
 */
    public static void displayByCompany() { 
	String company;
	System.out.print("Enter company: ");
	company = getInput();
	if (company.length() > 15) {
	    company = company.substring(0, 14);
	    System.out.println("Entered company name exceeds maximum length. It has been truncated to: " + company);
	}
	list.displayByCompany(company);
    }

/**
 * Displays all entries in the list with a name matching user's input. After getting the user' input, this method calls the CardList class' displayByName() method.
 */
    public static void displayByName() {
	String name;
	System.out.print("Enter name: ");
	name = getInput();
	if (name.length() > 25) {
	    name = name.substring(0, 24);
	    System.out.println("Entered name exceeds maximum length. It has been truncated to: " + name);
	}
	list.displayByPerson(name);
    }

/**
 * Displays all entries in the list with a status matching user's input. After getting the user' input, this method calls the CardList class' displayByStatus() method.
 */
    public static void displayByStatus() {
	int status;
	System.out.print("Enter status: ");
	try {
	    status = Integer.parseInt(getInput());
	}
	catch(Exception e) {
	    System.out.println("Please enter a valid integer value.");
	    return;
	}
	list.displayByStatus(status);
    }

/**
 * Displays the first entry in the list. This method calls the CardList class' displayTop() method.
 */
    public static void displayTop() {
	list.displayTop();
    }


/**
 * Displays the next entry in the list. This method calls the CardList class' displayNext() method.
 */
    public static void displayNext() {
	list.displayNext();
    }


/**
 * Displays the previous entry in the list. This method calls the CardList class' displayPrevious() method.
 */
    public static void displayPrev() {
	list.displayPrevious();
    }

/**
 * Provides a menu-driven system for removing an item from the list.
 *
 */ 
    public static void remove() {
	System.out.print("\nEnter name: ");
	String name = getInput();
	if (name.length() > 25) {
	    name = name.substring(0, 24);
	    System.out.println("Entered name exceeds maximum length. It has been truncated to: " + name);
	}
	System.out.print("Enter company:");
	String 	company = getInput();
	if (company.length() > 15) {
	    company = company.substring(0, 14);
	    System.out.println("Entered company name exceeds maximum length. It has been truncated to: " + company);
	}
	if (list.removeCardInfo(name, company))
	    System.out.println("\nCredit card data for " + name  + "for card issued by " + company + " has been removed.");
	else System.out.println("\nNo credit card data for " + name  + "for card issued by " + company + "was found.");
    }


    /**
 * Provides a way for the user to quit the program gracefully
 *
 */ 
    public static void quit() {
	System.out.println("\nTerminating program normally....\n\n");
	System.exit(0);
    }

   /**
 * A helper method that is called fomr the main menu when user's input is not recognized.
 *
 */
    public static void invalidInput() {
	System.out.println("\nInput not recognized. Please try agian.");
    }

/**
 * Provides a menu-driven system for manipulating the CreditCardReport. This method loops back 
 * to displaying the menu after every action the user takes.
 *
 */ 
    public static void main(String[] args) {
	list = new CardList();	
	while (true){
	    String str = displayMenu();
	   
	    if (str.equals("in"))
		addUser();
	    else if (str.equals("da"))
		displayAll();
	    else if (str.equals("db"))
		displayByBal();
	    else if (str.equals("dc"))
		displayByCompany();
	    else if (str.equals("dp"))
		displayByName();
	    else if (str.equals("ds"))
		displayByStatus();
	    else if (str.equals("rm"))
		remove();
	    else if (str.equals("tp"))
		displayTop();
	    else if (str.equals("pr"))
		displayPrev();
	    else if (str.equals("nx"))
		displayNext();
	    else if (str.equals("q"))
		quit();
	    else invalidInput();
	}
	
    }

}
